package com.antdesignbackend.backend.news.newsusers.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.antdesignbackend.backend.news.news.entity.News;
import com.antdesignbackend.backend.news.news.service.NewsService;
import com.antdesignbackend.backend.news.notice.entity.Notice;
import com.antdesignbackend.backend.news.noticeusers.entity.NoticeUsers;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.news.newsusers.entity.NewsUsers;
import com.antdesignbackend.backend.news.newsusers.entity.entityVo.NewsUsersVo;
import com.antdesignbackend.backend.news.newsusers.service.NewsUsersService;

/**
 * 消息推送
 *
 * @author 不二
 * @email newsusers
 * @date 2020-07-27 17:18:34
 */

@Controller
@RequestMapping("/newsUsers")
@Slf4j
public class NewsUsersController extends BaseController{

	private static final String FUN_NAME = "消息推送";//功能名称

	@Autowired
	private NewsUsersService newsUsersService;
	@Autowired
	private NewsService newsService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "news/newsUsers/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody NewsUsersVo newsUsersVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		List<NewsUsersVo> list = new ArrayList<>();
		QueryWrapper<NewsUsersVo> wrapper = new QueryWrapper<>();
		wrapper.eq("uid",getLoginUserId());
		if (newsUsersVo.getStatus() != null && newsUsersVo.getStatus() != -1){
			wrapper.eq("status",newsUsersVo.getStatus());
		}
		if (newsUsersVo.getTitle() != null && !newsUsersVo.getTitle().equals("")){
			wrapper.like("title",newsUsersVo.getTitle());
		}
		wrapper.orderByDesc("CreateTime");
		int total = newsUsersService.count(wrapper);
		if (total > 0){
			list = newsUsersService.search(wrapper,new Page<NewsUsersVo>(newsUsersVo.getPageIndex(),newsUsersVo.getPageSize()));
			if (list.size() > 0){
				for (NewsUsersVo newsUsersVo1:list){
					//去掉内容html标签，用于显示
					if (newsUsersVo1.getContent() != null){
						String showContent = newsUsersVo1.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");
						if (showContent.length() > 16){
							showContent = showContent.substring(0,16) + "...";
						}
						newsUsersVo1.setContent(showContent);
					}
				}
			}
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	@GetMapping("/read/{id}")
	@ResponseBody
	public	ReturnVO getById(@PathVariable(name = "id") String id) {
		ReturnVO returnVO = new ReturnVO();
		NewsUsers newsUsers = newsUsersService.getById(id);
		if (newsUsers != null){
			if (newsUsers.getStatus() == 0){
				newsUsers.setStatus(1);
				newsUsersService.updateById(newsUsers);
			}
			News news = newsService.getById(newsUsers.getNid());
			returnVO.setData(news);
		}
		return returnVO;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody NewsUsers newsUsers){
		ReturnVO result = new ReturnVO();
		if(newsUsersService.save(newsUsers)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody NewsUsers newsUsers){
		ReturnVO result = new ReturnVO();
		if(newsUsersService.updateById(newsUsers)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (newsUsersService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
